using System;
using System.Data;
using System.Text;
using System.Windows.Forms;

using ctsu.BaseForms;

namespace ctsu.Admin.Forms
{
	public class FormGrid : System.Windows.Forms.Form
	{
		public delegate void FormGridRowChangeEventHandler(object sender, FormGridRowChangeEventArgs e);

		public event FormGridRowChangeEventHandler FormGridRowChange;
		private Form _assocForm;
		private string _sql;
		private string _table;
		private bool _settingDataSource = false;
		private int _curRow;
		private System.Windows.Forms.DataGrid grid;

		private System.ComponentModel.Container components = null;

		public FormGrid(string sql, Form assocForm, string table)
		{
			InitializeComponent();

			this._assocForm = assocForm;
			this._sql = sql;
			this._table = table;

			SetDataSource(0);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.grid = new System.Windows.Forms.DataGrid();
			((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
			this.SuspendLayout();
			// 
			// grid
			// 
			this.grid.DataMember = "";
			this.grid.Dock = System.Windows.Forms.DockStyle.Fill;
			this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.grid.Location = new System.Drawing.Point(0, 0);
			this.grid.Name = "grid";
			this.grid.ReadOnly = true;
			this.grid.Size = new System.Drawing.Size(576, 273);
			this.grid.TabIndex = 0;
			this.grid.CurrentCellChanged += new System.EventHandler(this.grid_CurrentCellChanged);
			// 
			// FormGrid
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(576, 273);
			this.Controls.Add(this.grid);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FormGrid";
			((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		public void SetDataSource(int currentRow)
		{
			StringBuilder sb = new StringBuilder();
			_settingDataSource = true;

			DataTable dt = null;
			try
			{
				DataSet ds = Data.DataAccess.GetDataSet(_sql);
				dt = ds.Tables[0];
				dt.PrimaryKey = new DataColumn[] { dt.Columns["ID"] };
				grid.DataSource = dt;
			}
			catch (Exception ex)
			{
				//				sb.Append("Error setting grid data source!");
				//				sb.Append(Environment.NewLine);
				//				sb.Append("Error message: " + ex.Message);
				//bool b = ////ExceptionPolicy.HandleException(ex, "Exception Policy");
				//				MessageBox.Show(this, sb.ToString(), "Grid Data Source", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			finally
			{
				_settingDataSource = false;
			}

		}

		private void grid_CurrentCellChanged(object sender, System.EventArgs e)
		{
			if (_settingDataSource)
				return;

			if (_curRow != grid.CurrentCell.RowNumber)
			{
				_curRow = grid.CurrentCell.RowNumber;

				int id = (int)grid[_curRow, 0];

				FormGridRowChangeEventArgs ea = new FormGridRowChangeEventArgs(id);
				if (FormGridRowChange != null)
					this.FormGridRowChange(this, ea);
			}
		}

		public void OnBLTableChange(object sender, BLTableChangeEventArgs e)
		{
			//			FormBase fb = (FormBase)sender;
			//
			//			Console.WriteLine("OnBLTableChange In FormGrid");
			//			Console.WriteLine("  Form: " + fb.Name);
			//			Console.WriteLine("  TableName: " + e.TableName);
			//			Console.WriteLine("  Change: " + e.TableChange.ToString());

			int currentRow = 0;
			if (e is BLTableChangeAdvEventArgs)
				currentRow = ((BLTableChangeAdvEventArgs)e).CurrentRow;
			else
				Console.WriteLine("is BLTableChangeEventArgs");

			if (e.TableName == this._table)
				SetDataSource(currentRow);

		}
	}

	public class FormGridRowChangeEventArgs
	{
		public int ID;

		public FormGridRowChangeEventArgs(int id)
		{
			this.ID = id;

		}
	}
}
